<template>
  <div class="mend-list-container">
    <div class="searchBox">
      <Search
        :initData="initSearchData"
        @emitSearchData="getSearchData"
      ></Search>
    </div>

    <div class="tableBox">
      <CustomTable :searchData="searchData"></CustomTable>
    </div>
  </div>
</template>

<script>
import moment from "moment";
import { initUserInfo } from "@/unit/tool";
import CustomTable from "./components/table";
import Search from "./components/search";

function getRangeTime(value) {
  switch (value) {
    case 0:
      return [
        moment()
          .quarter(moment().quarter())
          .startOf("quarter"),
        moment()
          .quarter(moment().quarter())
          .endOf("quarter")
      ];
    case 1:
      return [
        moment()
          .quarter(moment().quarter() - 1)
          .startOf("quarter"),
        moment()
          .quarter(moment().quarter() - 1)
          .endOf("quarter")
      ];
    case 2:
      return [
        moment()
          .year(moment().year())
          .startOf("year"),
        moment()
          .year(moment().year())
          .endOf("year")
      ];
    case 3:
      return [
        moment()
          .year(moment().year() - 1)
          .startOf("year"),
        moment()
          .year(moment().year() - 1)
          .endOf("year")
      ];
    case 4:
      return [moment(), moment()];
    default:
      break;
  }
}

export default {
  name: "MendList",
  components: {
    CustomTable,
    Search
  },
  data() {
    return {
      userName: "",
      name: "",
      role: "",
      compId: "",
      secondDepartment: "",
      initSearchData: {},
      searchData: {}
    };
  },
  methods: {
    getSearchData(value) {
      this.searchData = value;
    }
  },
  created() {
    initUserInfo(this);
    let rangeTime = getRangeTime(0);
    let startTime = moment(rangeTime[0]).format("YYYY-MM-DD");
    let endTime = moment(rangeTime[1]).format("YYYY-MM-DD");
    let { secondDepartment } = JSON.parse(localStorage.getItem("userInfo"));
    this.initSearchData = {
      district: secondDepartment,
      timeType: 0,
      startTime,
      endTime,
      rangeTime
    };
    this.searchData = Object.assign(this.searchData, this.initSearchData);
  },
  mounted() {}
};
</script>

<style scoped="scoped">
.searchBox {
  margin-bottom: 16px;
}

.tableBox {
  padding: 16px;
  background-color: #ffffff;
}
</style>
